package com.example.weddingapp.fragment;

import android.content.Context;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;

import com.bumptech.glide.Glide;
import com.example.weddingapp.R;
import com.example.weddingapp.base.BaseFragment;
import com.example.weddingapp.databinding.FragmentHomeBinding;

import java.util.ArrayList;
import java.util.List;

import cn.kevin.banner.BannerAdapter;
import cn.kevin.banner.IBannerItem;
import cn.kevin.banner.ImageLoader;

public class HomeFragment extends BaseFragment<AndroidViewModel, FragmentHomeBinding> {
    @Override
    public int setContent() {
        return R.layout.fragment_home;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showContentView();
        initView();

        initWebView();
    }

    private void initWebView() {
//        String url = "https://map.baidu.com/mobile/webapp/index/index#index/index/foo=bar/vt=map";
        String url = "https://ditu.amap.com/?src=uriapi";
        bindingView.webView.getSettings().setJavaScriptEnabled(true);
        bindingView.webView.loadUrl(url);
        bindingView.webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //使用WebView加载显示url
                view.loadUrl(url);
                //返回true
                return true;
            }

        });
    }

    private void initView() {

        BannerAdapter adapter = new BannerAdapter(new GlideImageLoader());
        //item需要实现IBannerItem接口
        List<IBannerItem> list = new ArrayList<>();
        IBannerItem item = new IBannerItem() {
            @Override
            public String ImageUrl() {
                return "https://img.zcool.cn/community/0101ee59118bc2b5b3086ed47c65a7.jpg@2o.jpg";
            }
        };

        list.add(item);
        item = new IBannerItem() {
            @Override
            public String ImageUrl() {
                return "https://img.zcool.cn/community/01cdaa5b471159a80121ade056d7a7.jpg@2o.jpg";
            }
        };

        list.add(item);

        item = new IBannerItem() {
            @Override
            public String ImageUrl() {
                return "https://img.zcool.cn/community/01910b5b471159a8012036be6893ca.jpg@2o.jpg";
            }
        };

        list.add(item);

        adapter.setData(getContext(), list);

        bindingView.viewPager.setBannerAdapter(adapter);
    }

    //这里举例为Glide,实际使用时需配合自己项目中的图片加载框架完成图片加载
    class GlideImageLoader implements ImageLoader {
        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
            Glide.with(context).load(url).into(imageView);
        }
    }
}
